using System.Data.SQLite;
namespace 第六次作业
{
    public partial class Form1 : Form
    {
        Dictionary<string, string> log = new Dictionary<string, string>();
        public Form1()
        {
            InitializeComponent();
            toolStripStatusLabel3.Text = DateTime.Now.ToString("G");
            timer1.Start();
            Table();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            toolStripStatusLabel3.Text = DateTime.Now.ToString("G");
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            Application.Exit();
        }
        //让表显示数据
        public void Table()
        {
            dataGridView1.Rows.Clear();
            string sql = "select * from student_information";
            Dao dao = new Dao();
            SQLiteDataReader reader = dao.read(sql);
            while (reader.Read())
            {
                string school,id,name,classs,age,grade;
                school = reader["school"].ToString();
                id = reader["id"].ToString();
                name = reader["name"].ToString();
                classs = reader["class"].ToString();
                age = reader["age"].ToString();
                grade = reader["grade"].ToString();
                string[] str = {school,id,name,classs,age,grade};
                dataGridView1.Rows.Add(str);
            }
            reader.Close();
        }

        private void 添加学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form21 f = new Form21();
            f.ShowDialog();
            log.Add(toolStripStatusLabel3.Text, "添加学生信息");
        }

        private void 修改学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string[] str = { dataGridView1.SelectedCells[0].Value.ToString(), dataGridView1.SelectedCells[1].Value.ToString(), 
                dataGridView1.SelectedCells[2].Value.ToString(),dataGridView1.SelectedCells[3].Value.ToString(),
            dataGridView1.SelectedCells[4].Value.ToString(),dataGridView1.SelectedCells[5].Value.ToString()};
            Form21 f = new Form21(str);
            f.ShowDialog();
            log.Add(toolStripStatusLabel3.Text, "修改学生信息");
        }

        private void 删除学生信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult r = MessageBox.Show("确定要删除选中信息吗？", "提示", MessageBoxButtons.OKCancel);
            if(r == DialogResult.OK)
            {
                string id, name;
                id = dataGridView1.SelectedCells[1].Value.ToString();
                name = dataGridView1.SelectedCells[2].Value.ToString();
                string sql = "delete from student_information where id='" + id + "'and name='" + name + "'";
                Dao dao = new Dao();
                dao.Execute(sql);
                MessageBox.Show("删除成功！");
                Table();
                log.Add(toolStripStatusLabel3.Text, "删除学生信息");
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Table();
        }

        private void 用户日志ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 form2 = new Form2(log);
            form2.Show();
        }
    }
}